﻿using System;
using System.IO;
using System.Diagnostics;

namespace MusicBrowser
{
    public class DirectoryTreeNode : TreeNode
    {

       
        public new string FullPath
        {
            get { return _fullPath; }
            set
            {
                _fullPath = value;
                HasChildNodes = (Directory.Exists(_fullPath) &&
                                 Directory.GetDirectories(_fullPath).Length > 0);
            }
        }

        public DirectoryTreeNode(String title, String fullPath, TreeView treeView)
            : base(title, fullPath)
        {
            FullPath = fullPath;
            TreeView = treeView;
            TreeView.CheckedNodeChanged +=
                new EventHandler<TreeNodeEventArgs>(TreeView_OnCheckedNodeChanged);
        }

        public override void GetChildNodes()
        {
            if (!String.IsNullOrEmpty(FullPath))
            {
                ChildNodes.Clear();
                string[] directories = Directory.GetDirectories(FullPath);

                foreach (string directory in directories)
                {
                    try
                    {
                        DirectoryInfo info = new DirectoryInfo(directory);
                        if ((info.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                        {
                            DirectoryTreeNode node =
                                new DirectoryTreeNode(Path.GetFileName(directory), directory, TreeView);
                            node.Level = Level + 1;
                            node.TreeView = TreeView;
                            TreeView.CheckedNodeChanged +=
                                new EventHandler<TreeNodeEventArgs>(TreeView_OnCheckedNodeChanged);
                            node.HasChildNodes = (Directory.GetDirectories(node.FullPath).Length > 0);
                            if (Config.Instance.initialFolders.Contains(node.FullPath)) node.Checked.Value = true;
                            ChildNodes.Add(node);
                        }
                    }
                    catch 
                    {
                        
                    }
                   
                }
                HasChildNodes = (ChildNodes.Count > 0);
            }

            base.GetChildNodes();
        }
        public override string ToString()
        {
            return FullPath;
        }

        private void TreeView_OnCheckedNodeChanged(object sender, TreeNodeEventArgs e)
        {
            
            Checked.Value = (e.Node == this);
        }

        private string _fullPath = String.Empty;
    }
}